package FinalProject

class SiteUserController {

    def index = {
        def users = SiteUser.list()
        def id = users[0].id
        redirect(action: 'show', id: id)
    }

    def show = {
        long id = params.id as Long
        def user = SiteUser.get(id)
        if (user == null) {
            throw new IllegalArgumentException("no user with id ${id}")
        }
        render(view : 'show', model : [user : user, viewType: "SiteUser"])
    }

    def showGame = {
        long id = params.id as Long
        def game = Game.get(id)
        if (game == null) {
            throw new IllegalArgumentException("No game with id ${id}")
        }
        render(view : 'show', model : [game: game,  viewType: "Game"])
    }

    def showPlatform = {
        long id = params.id as Long
        def platform = Platform.get(id)
        if (platform == null) {
            throw new IllegalArgumentException("No game with id ${id}")
        }
        render(view : 'show', model : [platform: platform, viewType: "Platform"])
    }

    def showAllUsers = {
        render(view : 'showAll', model : [viewType:  "All Users", users: SiteUser.list()])
    }

    def showAllGames = {
        render(view : 'showAll', model : [viewType: "All Games", games: Game.list()])
    }

    def showAllPlatforms = {
        render(view : 'showAll', model : [viewType:  "All Platforms", platforms: Platform.list()])
    }

    def makeNew = {
        if (params.type == "Game") {
            def game = Game.get(params.id)
            if (game == null) {
                throw new IllegalArgumentException("no game with id ${params.id}")
            }
            def user = SiteUser.findAllByName(params.userName.trim())
            if (user.size() == 0)  {
                def newUser = new SiteUser(name: params.userName, dateJoined: new Date())
                newUser.post(params.update, game)
                newUser.save(failOnError: true, flush: true)
            }
            else {
                user.get(0).post(params.update, game)
                user.get(0).save(failOnError: true, flush:  true)
            }
            redirect(action: 'showGame', model: [game: game, viewType: "Game"], id: params.id)
        }
        else if (params.type == "Platform") {
            def platform = Platform.get(params.id)
            if (platform == null) {
                throw new IllegalArgumentException("no platform with id ${params.id}")
            }
            /**def game = new Game(name: params.gameName, releaseDate: params.releaseDate, platform: platform)
            platform.addToGames(game)
            platform.save(failOnError: true, flush:  true)
            redirect(action: 'showPlatform', model: [platform: platform, viewType: "Platform"], id: params.id)**/
            def games = Game.findAllByName(params.gameName.trim())
            boolean exists = false
            if (games.size() != 0) {
                for (game in games) {
                    if (game.platform.id == params.id) {
                        exists = true
                    }
                }
            }
            if (!exists) {
                def game = new Game(name: params.gameName, releaseDate: params.releaseDate, platform: platform)
                platform.addToGames(game)
                platform.save(failOnError: true, flush: true)
            }
            redirect(action: 'showPlatform', model: [platform: platform, viewType: "Platform"], id: params.id)
        }
        else if (params.type == "AllPlatforms")     {
            def platform = Platform.findByName(params.platformName.trim())
            if (platform == null) {
                platform = new Platform(name: params.platformName, dateOfRelease: params.releaseDate)
                platform.save(failOnError: true, flush: true)
            }
            redirect(action: 'showAllPlatforms', model: [platform: platform, viewType: "Platform"])
        }
    }
}
